/**
 * inforcreation.com co.Ltd
 * MichaelHuang
 * 2012-3-31
 */
package com.zchat.android.views;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.zchat.android.R;
import com.zchat.android.views.menus.MenuFive;
import com.zchat.android.views.menus.MenuFour;
import com.zchat.android.views.menus.MenuItem;
import com.zchat.android.views.menus.MenuOne;
import com.zchat.android.views.menus.MenuThree;
import com.zchat.android.views.menus.MenuTwo;
import com.zchat.api.util.Resolution;

/**
 * 底部菜单按钮
 * @author MichaelHuang
 *
 */
public class MenuBottom extends LinearLayout {
	/**
	 * @param context
	 */
	public MenuBottom(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public MenuBottom(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}
	
	/** 记录当前选择的按钮 */
	public static int pos = 0;
	
	private MenuItem[] items;
	/**
	 * 实例化组件
	 * @param context
	 */
	private void init(Context context) {
		// TODO Auto-generated method stub
		this.setBackgroundResource(R.drawable.bg_bottom_bar);
		this.setGravity(Gravity.CENTER);
		
		items = new MenuItem[5];
		items[0] = new MenuOne(context, R.drawable.bg_menu_active, R.drawable.menu_one_normal, R.drawable.menu_one_active, R.string.menu_one, Color.BLACK, Color.WHITE);
		items[0].setTag("0");
		items[1] = new MenuTwo(context, R.drawable.bg_menu_active, R.drawable.menu_two_normal, R.drawable.menu_two_active, R.string.menu_two, Color.BLACK, Color.WHITE);
		items[1].setTag("1");
		items[2] = new MenuThree(context, R.drawable.bg_menu_active, R.drawable.menu_three_normal, R.drawable.menu_three_active, R.string.menu_three, Color.BLACK, Color.WHITE);
		items[2].setTag("2");
		items[3] = new MenuFour(context, R.drawable.bg_menu_active, R.drawable.menu_four_normal, R.drawable.menu_four_active, R.string.menu_four, Color.BLACK, Color.WHITE);
		items[3].setTag("3");
		items[4] = new MenuFive(context, R.drawable.bg_menu_active, R.drawable.menu_five_normal, R.drawable.menu_five_active, R.string.menu_five, Color.BLACK, Color.WHITE);
		items[4].setTag("4");
		
		for(final MenuItem item : items) {
			this.addView(item, Resolution.getInstance().getScreenWidth() / 5, LayoutParams.FILL_PARENT);
			item.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					String tag = item.getTag().toString();
					
					if(tag.equals(pos + ""))
					{
						return;
					}
					
					for(int i = 0;i < items.length;i++)
					{
						if(v.getTag().equals(i+""))
						{
							items[i].toActive();
							items[i].onClick();
							items[pos].toNormal();
							pos = i;
							break;
						}
					}
				}
			});
		}
		
//		SharedPreferences preferences = context.getSharedPreferences(
//				System.getProperty(SystemProperty.APP_PREFERENCE), Context.MODE_PRIVATE);
//		
//		String userhabit = preferences.getString("userhabit","0");
		
//		items[Integer.valueOf(userhabit)].toActive();
		
		items[0].toActive();
		
	}
	
	public MenuItem[] getMenuItems()
	{
		return items;
	}
}
